package com.lywz.biz.service.food.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lywz.biz.common.dao.CommonDao;
import com.lywz.biz.common.entity.goodfood.Food;
import com.lywz.biz.dao.food.FoodDao;
import com.lywz.biz.service.food.FoodService;
import com.lywz.core.exception.ExceptionMes;
import com.lywz.core.exception.SSException;
import com.lywz.core.log.LogClerk;

/**
 * 
 * @author qianqi yangbolin
 * @date 2010-4-28
 */
@Service("foodService")
public class FoodServiceImpl implements FoodService {

    @Autowired
    private CommonDao commonDao;

    @Autowired
    private FoodDao foodDao;

    @Override
    public Food getOneFood(Integer foodId) throws SSException {
        try {
            return (Food) commonDao.queryById(Food.class, foodId);
        } catch (Exception e) {
            e.printStackTrace();
            throw SSException.get(ExceptionMes.SYSEXCEPTION);
        }
    }

    @Override
    public List<Food> getDetailFoodListByFoodType(Integer foodType, int currentPage, int pageSize) throws SSException {
        try {
            return foodDao.getFoodByType(foodType, currentPage, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            throw SSException.get(ExceptionMes.SYSEXCEPTION);
        }
    }

    @Override
    public boolean insertNewFood(Food food) {
        try {
            commonDao.insert(food);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public List<Food> getTypedFoodTypeList(Integer foodType, int limit) {
        try {
            List<Food> specialFood = foodDao.getTypedFoodList(foodType, limit);
            return specialFood;
        } catch (Exception e) {
            return null;
        }

    }

    @Override
    public List<Food> getTopSpecialFoodList(int limit) {
        try {
            List<Food> specialFoodTopList = foodDao.getTopSpecialFoodList(limit);
            return specialFoodTopList;
        } catch (Exception e) {
            return null;
        }

    }

    @Override
    public List<Food> getOtherFoodTypeList(Integer foodType, int limit) {
        try {
            List<Food> otherFoodList = foodDao.getOtherFoodList(foodType, limit);
            return otherFoodList;
        } catch (Exception e) {
            return null;
        }
    }

    /* (non-Javadoc)
     * @see com.lywz.biz.service.food.FoodService#getAllTypedFoodListByAddTime(int)
     */
    @Override
    public List<Food> getAllTypedFoodListByAddTime(int foodType) throws SSException {
        try {
            return foodDao.getAllTypedFoodListByAddTime(foodType);
        }catch(Exception e) {
            e.printStackTrace();
            LogClerk.sysout.error(e);
            throw SSException.get(ExceptionMes.SYSEXCEPTION);
        }
    }

    @Override
    public boolean deleteOneFoodById(int foodId) throws SSException {
        try{
            commonDao.delete(getOneFood(foodId));
            return true;
        }
        catch (Exception e) {
            LogClerk.sysout.error(e);
            throw SSException.get(ExceptionMes.SYSEXCEPTION);
        }
        
    }

    @Override
    public boolean updateOneFood(Food food) throws SSException {
        try {
            commonDao.update(food);
            return true;
        }catch(Exception e) {
            e.printStackTrace();
            LogClerk.sysout.error(e);
            throw SSException.get(ExceptionMes.SYSEXCEPTION);
        } 
    }

    @Override
    public List<Food> getSpecialOrDrinkTeaListOrderByTime(boolean flag) throws SSException {
        try {
            if(flag) {
                return foodDao.getAllSpecialFoodListByAddTime();
            }else
                return foodDao.getDrinkTeaListOrderByAddTime();
        } catch (Exception e) {
            e.printStackTrace();
            LogClerk.sysout.error(e);
            throw SSException.get(ExceptionMes.SYSEXCEPTION);
        }
    }
    
}
